package com.qf.sl.youshare.activity;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.widget.TextView;
import com.qf.sl.youshare.DataStorage;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.adapters.CommonFragmentAdapter;
import com.qf.sl.youshare.fragments.StorePlatformFragment;
import com.qf.sl.youshare.fragments.StoreCoreFragment;
import com.qf.sl.youshare.model.Category;
import com.qf.sl.youshare.transform.ZoomOutPageTransformer;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/4.
 * 19:41
 */

/**
 * 商城界面部分的 Activity
 */
public class StoreActivity
        extends FragmentActivity
        implements ActionBar.TabListener,
                   ViewPager.OnPageChangeListener {

    private ViewPager pager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);

        //ActionBar 调整样式

        ActionBar actionBar = getActionBar();

        if (actionBar != null) {

            //设置ActionBar的 Tag 模式
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            //设置返回按钮
            actionBar.setDisplayHomeAsUpEnabled(true);

            //去掉 ActionBar 上的图标
            actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

            //标题居中
            actionBar.setDisplayShowCustomEnabled(true);

            //从 layout 文件夹中加载布局,然后通过代码修改标题
            LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.actionbar_main_title, null);

            //获取 view 中的 TextView 进行标题的设置
            TextView textView = (TextView) view.findViewById(R.id.actionbar_title);
            if (textView != null) {
                textView.setText("商城");
            }

            actionBar.setCustomView(
                    view,
                    new ActionBar.LayoutParams(
                            Gravity.CENTER_HORIZONTAL)
                );

            actionBar.setBackgroundDrawable(new ColorDrawable(0xFF1194D3));
//            actionBar.setBackgroundDrawable(new ColorDrawable(R.color.store_bg));

            actionBar.setDisplayShowTitleEnabled(false);

            pager = (ViewPager) findViewById(R.id.store_pager);

            DataStorage dataStorage = DataStorage.getInstance();

            Category firstCategory = dataStorage.getCategory("1");
            Category secondCategory = dataStorage.getCategory("2");

            //增加Tag
            ActionBar.Tab tab = actionBar.newTab();

            if (firstCategory != null) {
                String name = firstCategory.getName();
                if (name != null) {
                    tab.setText(name);
                }else {
                    tab.setText("核心产品");
                }
            }else {
                tab.setText("核心产品");
            }

            tab.setTag("1");
            tab.setTabListener(this);
            actionBar.addTab(tab);

            tab = actionBar.newTab();

            if (secondCategory != null) {
                String name = secondCategory.getName();
                if (name != null) {
                    tab.setText(name);
                }else {
                    tab.setText("平台产品");
                }
            }else {
                tab.setText("平台产品");
            }

            tab.setTag("2");
            tab.setTabListener(this);
            actionBar.addTab(tab);

            //对于ActionBar Tag 与 ViewPager 联动, 需要提前创建
            //ViewPager 实例, 在第一个 addTag 之前创建
            LinkedList<Fragment> fragments = new LinkedList<Fragment>();

            //添加相应的Fragment
            //添加核心产品
            fragments.add(new StoreCoreFragment());
            //添加平台产品
            fragments.add(new StorePlatformFragment());

            CommonFragmentAdapter adapter =
                    new CommonFragmentAdapter(
                            getSupportFragmentManager(),
                            fragments
                    );
            pager.setAdapter(adapter);
            pager.setPageTransformer(true,new ZoomOutPageTransformer());
            pager.setOnPageChangeListener(this);

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.store, menu);

        return true;
    }

    /**
     * actionBar中菜单键的监听
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int featureId = item.getItemId();

        switch (featureId){
            case R.id.action_register://点击注册菜单
                Intent registerIntent = new Intent(this, UserRegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.action_login:
                Intent loginIntent = new Intent(this, LoginActivity.class);
                startActivity(loginIntent);
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    //////////////// ActionBar 中 Tag 的点击事件; /////////////////

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (pager != null) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null) {
                int selectedIndex = actionBar.getSelectedNavigationIndex();
                pager.setCurrentItem(selectedIndex);
            }
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    ////////////////// ViewPager 页面切换进行的事物处理 ///////////////////

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setSelectedNavigationItem(position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}